package com.yiren.dbaa.util;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

import androidx.core.app.ActivityCompat;

import com.yiren.dbaa.view.dialog.CustomDialog;
import com.yiren.dbaa.view.dialog.LoadDialog;

import java.util.List;

/**
 * Created by WANGRONGXIANG
 * on 2022/2/22
 * Describe
 */
public class NetWorkUtil {

    /**
     * 判断网络连接是否可用
     */
    public static boolean isNetworkConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {//如果仅仅是用来判断网络连接　　　　　  //则可以使用 cm.getActiveNetworkInfo().isAvailable();
            NetworkInfo[] info = cm.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 检测网络是否连接
     */
    public static boolean isNetworkAvailable(Context context) {
        try {
            // 得到网络连接信息
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            // 去进行判断网络是否连接
            if (manager.getActiveNetworkInfo() != null) {
                return manager.getActiveNetworkInfo().isAvailable();
            }
        } catch (Exception e) {
            return false;
        }
        return false;
    }

    /**
     * 判断网络连接是否打开
     */
    public static boolean networkAvailable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null && cm.getActiveNetworkInfo().isAvailable()) {//如果仅仅是用来判断网络连接　　　　　  //则可以使用 cm.getActiveNetworkInfo().isAvailable();
            return true;
        }
        return false;
    }

    /**
     * 判断GPS是否打开
     */
    public static boolean isGpsEnabled(Context context) {
        LocationManager lm = ((LocationManager) context.getSystemService(Context.LOCATION_SERVICE));
        List<String> accessibleProviders = lm.getProviders(true);
        return accessibleProviders != null && accessibleProviders.size() > 0;
    }

    /**
     * 判断WIFI是否打开
     */
    public static boolean isWifiEnabled(Context context) {
        ConnectivityManager mgrConn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        TelephonyManager mgrTel = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
//            return TODO;
        }
        return ((mgrConn.getActiveNetworkInfo() != null && mgrConn
                .getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
    }

    /**
     * 判断是否是3G网络(手机数据流量)
     */
    public static boolean is3rd(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkINfo = cm.getActiveNetworkInfo();
        if (networkINfo != null
                && networkINfo.getType() == ConnectivityManager.TYPE_MOBILE) {
            return true;
        }
        return false;
    }

    /**
     * 判断是wifi还是3g网络,用户的体现性在这里了，wifi就可以建议下载或者在线播放
     */
    public static boolean isWifi(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkINfo = cm.getActiveNetworkInfo();
        if (networkINfo != null
                && networkINfo.getType() == ConnectivityManager.TYPE_WIFI) {
            return true;
        }
        return false;
    }

    /**
     * 网络监测提示
     */
    public static void showNetErrorTips(final Activity mActivity) {
        CustomDialog.Builder builder = new CustomDialog.Builder(mActivity);
        builder.setMessage("您的网络处于断开的状态\n练车完成时，请在网络通畅的状态下点击结束练车\n否则系统会持续消耗您的练车可用时间");
        builder.setTitle("温馨提示");
        builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setPositiveButton("检查网络", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                if (android.os.Build.VERSION.SDK_INT > 10) {
                    // 3.0以上打开设置界面，也可以直接用ACTION_WIRELESS_SETTINGS打开到wifi界面
                    mActivity.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
                } else {
                    mActivity.startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
                }
            }
        });
        builder.create().show();
    }

    /**
     * 网络监测提示
     */
    public static void showNetErrorTips(final Activity mActivity, DialogInterface.OnClickListener listener) {
        CustomDialog.Builder builder = new CustomDialog.Builder(mActivity);
        builder.setMessage("您的网络处于断开的状态\n练车完成时，请在网络通畅的状态下点击结束练车\n否则系统会持续消耗您的练车可用时间");
        builder.setTitle("温馨提示");
        builder.setCanceled(false);
        builder.setNegativeButton("继续练车", listener);
        builder.setPositiveButton("检查网络", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                LoadDialog.dismiss(mActivity);
                dialog.dismiss();
                if (android.os.Build.VERSION.SDK_INT > 10) {
                    // 3.0以上打开设置界面，也可以直接用ACTION_WIRELESS_SETTINGS打开到wifi界面
                    mActivity.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
                } else {
                    mActivity.startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
                }
            }
        });
        builder.create().show();
    }
}
